<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号" prop="orderNumber">
      <el-input v-model="dataForm.orderNumber" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="创建订单时间" prop="orderCreatedate">
      <el-input v-model="dataForm.orderCreatedate" placeholder="创建订单时间"></el-input>
    </el-form-item>
    <el-form-item label="订单金额" prop="orderPrice">
      <el-input v-model="dataForm.orderPrice" placeholder="订单金额"></el-input>
    </el-form-item>
    <el-form-item label="审核表id" prop="examineId">
      <el-input v-model="dataForm.examineId" placeholder="审核表id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="订单描述" prop="orderDescribe">
      <el-input v-model="dataForm.orderDescribe" placeholder="订单描述"></el-input>
    </el-form-item>
    <el-form-item label="订单类型（1.二手房发布，2.出租发布，3.买房发布，4.租房发布）" prop="oriderType">
      <el-input v-model="dataForm.oriderType" placeholder="订单类型（1.二手房发布，2.出租发布，3.买房发布，4.租房发布）"></el-input>
    </el-form-item>
    <el-form-item label="订单状态（1.未支付，2.已支付，3.已取消）" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态（1.未支付，2.已支付，3.已取消）"></el-input>
    </el-form-item>
    <el-form-item label="房源id" prop="houseId">
      <el-input v-model="dataForm.houseId" placeholder="房源id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          orderNumber: '',
          orderCreatedate: '',
          orderPrice: '',
          examineId: '',
          userId: '',
          orderDescribe: '',
          oriderType: '',
          orderStatus: '',
          houseId: ''
        },
        dataRule: {
          orderNumber: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          orderCreatedate: [
            { required: true, message: '创建订单时间不能为空', trigger: 'blur' }
          ],
          orderPrice: [
            { required: true, message: '订单金额不能为空', trigger: 'blur' }
          ],
          examineId: [
            { required: true, message: '审核表id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          orderDescribe: [
            { required: true, message: '订单描述不能为空', trigger: 'blur' }
          ],
          oriderType: [
            { required: true, message: '订单类型（1.二手房发布，2.出租发布，3.买房发布，4.租房发布）不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态（1.未支付，2.已支付，3.已取消）不能为空', trigger: 'blur' }
          ],
          houseId: [
            { required: true, message: '房源id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/order/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderNumber = data.order.orderNumber
                this.dataForm.orderCreatedate = data.order.orderCreatedate
                this.dataForm.orderPrice = data.order.orderPrice
                this.dataForm.examineId = data.order.examineId
                this.dataForm.userId = data.order.userId
                this.dataForm.orderDescribe = data.order.orderDescribe
                this.dataForm.oriderType = data.order.oriderType
                this.dataForm.orderStatus = data.order.orderStatus
                this.dataForm.houseId = data.order.houseId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/order/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'orderNumber': this.dataForm.orderNumber,
                'orderCreatedate': this.dataForm.orderCreatedate,
                'orderPrice': this.dataForm.orderPrice,
                'examineId': this.dataForm.examineId,
                'userId': this.dataForm.userId,
                'orderDescribe': this.dataForm.orderDescribe,
                'oriderType': this.dataForm.oriderType,
                'orderStatus': this.dataForm.orderStatus,
                'houseId': this.dataForm.houseId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
